﻿Ext.define("App.View.Compras.FormCompra", {
    extend: "App.Config.Abstract.Form",
    columns: 1,
    initComponent: function () {
        var me = this;
        me.CargarComponentes();
        me.cargarEventos();
        this.callParent(arguments);
    },
    CargarStore : function(){
        var me = this;
        me.store_combustible.load();
        me.store_cuenta.load();
        me.store_tipo.load();
    },
    CargarComponentes: function () {
        var me = this;
        me.txt_nro_cmp = Ext.create("App.Config.Componente.TextFieldBase", {
            fieldLabel: "Nro Comprobante",
            readOnly : true,
            name: "NRO_COMP",

        });
         me.date_fecha = Ext.create("App.Config.Componente.DateFieldBase", {
            fieldLabel: "Fecha",
            name: "FECHA",
            afterLabelTextTpl: Constantes.REQUERIDO,
            allowBlank: false,
        });
        me.store_combustible = Ext.create('App.Store.Combustibles.Combustibles').load();
        me.cbx_combustible = Ext.create("App.Config.Componente.ComboBase", {
            fieldLabel: "Combustible",
            name: "ID_COMBUSTIBLE",
            displayField: 'NOMBRE',
            valueField : 'ID_COMBUSTIBLE',
            store: me.store_combustible,
            colspan : 2,
            afterLabelTextTpl: Constantes.REQUERIDO,
            allowBlank: false,
            textoTpl : function () { return "{NOMBRE} - {DESCRIPCION}" }
        });
        me.store_cuenta = Ext.create('App.Store.Cajas.Cajas').load();
        me.cbx_cuenta = Ext.create("App.Config.Componente.ComboBase", {
            fieldLabel: "Origen Cuenta",
            name: "ID_CAJA",
            displayField: 'NOMBRE',
            valueField : 'ID_CAJA',
            store: me.store_cuenta,
            afterLabelTextTpl: Constantes.REQUERIDO,
            allowBlank: false,
            textoTpl : function () { return "{NOMBRE} - SALDO : {SALDO}" }
        });
       
        me.num_cantidad = Ext.create("App.Config.Componente.NumberFieldBase", {
            fieldLabel: "Cantidad",
            name: "CANTIDAD",
            allowDecimals: true,
            maxValue: 999999999,
            afterLabelTextTpl: Constantes.REQUERIDO,
            allowBlank: false,
        });
        me.num_nro_factura = Ext.create("App.Config.Componente.NumberFieldBase", {
            fieldLabel: "Nro Factura",
            name: "NRO_FACTURA",
            allowDecimals: true,
            maxValue: 99999999999,
            afterLabelTextTpl: Constantes.REQUERIDO,
            allowBlank: false,
        });

        
        me.store_tipo = Ext.create('App.Store.Listas.StoreLista');
        me.store_tipo.setExtraParam('ID_LISTA', Lista.Buscar('TIPO_COMPRA'));
        me.cbx_tipo = Ext.create("App.Config.Componente.ComboBase", {
            fieldLabel: "Tipo",
            name: "TIPO",
            displayField: 'VALOR',
            valueField: 'VALOR',
            store: me.store_tipo,
            afterLabelTextTpl: Constantes.REQUERIDO,
            allowBlank: false
        });
        me.num_precio = Ext.create("App.Config.Componente.NumberFieldBase", {
            fieldLabel: "Precio",
            name: "PRECIO",
            allowDecimals: true,
            maxValue: 999999999,
        });
        me.num_importe = Ext.create("App.Config.Componente.NumberFieldBase", {
            fieldLabel: "Importe",
            name: "IMPORTE",
            allowDecimals: true,
            maxValue: 999999999,
            readOnly : true
//            afterLabelTextTpl: Constantes.REQUERIDO,
//            allowBlank: false
        });
        me.num_formulario = Ext.create("App.Config.Componente.NumberFieldBase", {
            fieldLabel: "Formulario",
            name: "FORMULARIO",
            allowDecimals: true,
            maxValue: 999999999,
            afterLabelTextTpl: Constantes.REQUERIDO,
            allowBlank: false
        });
          me.num_total = Ext.create("App.Config.Componente.NumberFieldBase", {
            fieldLabel: "Total",
            name: "TOTAL",
            allowDecimals: true,
            maxValue: 999999999,
            readOnly : true
        });
        me.gridDetalle = Ext.create("App.View.Compras.Grids",{
            opcion : 'GridDetallesCompra',
            colspan : 2,
            width : 480,
            height : 200,
            handler : me.EliminarDetalle,
            scope : me
        });
         me.toolbarDetalle = Funciones.CrearMenuBar();
        Funciones.CrearMenu('btn_Crear', 'Crear', Constantes.ICONO_CREAR, me.EventosDetalle, me.toolbarDetalle, this);
        me.gridDetalle.addDocked(me.toolbarDetalle, 1);
        me.items = [
            me.txt_nro_cmp,
            me.cbx_tipo,
            me.date_fecha,
            me.num_precio,
            me.cbx_combustible,
            me.cbx_cuenta,
            me.num_nro_factura,
            me.num_cantidad,
//            me.num_formulario,
            me.num_importe,
            me.gridDetalle,
            me.num_total
        ];
       
      

    },
    ModoEdicion : function(){
        var me = this;
        me.cbx_combustible.setReadOnly(true);
        me.cbx_cuenta.setReadOnly(true);
        me.date_fecha.setReadOnly(true);
    },
    EventosDetalle : function(btn){
        var me = this;
        if(btn.getItemId() == "btn_Crear"){
            var rec = Ext.create('App.Model.Compras.DetallesCompra',{
                DETALLE : 'DETALLE',
                IMPORTE : 1,
            });
            me.gridDetalle.getStore().add(rec);  
        }
        else{
            Ext.Msg.alert("Error","No existe opcion"+btn.getItemId() );
        }
    },
    EliminarDetalle : function(grid, rowIndex, colIndex){
        var me = this;
        var rec = grid.getStore().getAt(rowIndex);
        if(rec.get('ID_DETALLE')== 0){
             grid.getStore().removeAt(rowIndex);
             me.CargarTotales();
        }
        else{
            
            Funciones.AjaxRequestGrid("Compras", "EliminarDetalleCompra", me, "Esta Seguro de Eliminar este Registro", { ID_DETALLE : rec.get('ID_DETALLE') }, grid, null);
//            alert("Eliminar BASE DATOS");
        }
        
    },
    cargarEventos : function(){
        var me = this;
        me.cbx_combustible.on('select',function(cmb,record){
            me.num_precio.reset();
            me.num_precio.setValue(record[0].get('PRECIO_COMPRA'));
        });
        me.num_precio.on('change',function(num,newvalue,oldvalue){
//            var sum = me.num_saldo.getValue() + newvalue;
            var cant = me.num_cantidad.getValue() == null? 0 : me.num_cantidad.getValue() ;
            me.num_importe.setValue(cant * newvalue);
            me.CargarTotales();
        });
        me.num_cantidad.on('change',function(num,newvalue,oldvalue){
//            var sum = me.num_saldo.getValue() + newvalue;
            var prec = me.num_precio.getValue() == null? 0 : me.num_precio.getValue() ;
            me.num_importe.setValue(prec * newvalue);
            me.CargarTotales();
        });
         me.gridDetalle.on('edit', function(editor, e){
            
            if (e.field == "IMPORTE"){
//                    e.record.set('TOTAL',e.value - e.record.get('ENT_LITTER') );
                    me.CargarTotales();
            }
        });
        me.gridDetalle.getStore().on('load',function(){
            me.CargarTotales();
        });
    },
    CargarTotales : function(){
        var me = this;
        
        var total = 0;
        me.gridDetalle.getStore().each(function(record){
//            alert(record.get('TOTAL'));
            total= total +  record.get('IMPORTE');

        });
        me.num_total.setValue(total + me.num_importe.getValue() );

    },
});
